package com.atguigu.gmall.list.listener;

import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.list.service.ListService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author gsy
 * @date 2021年09月13日17:07
 * Description
 */
@Component
@Slf4j
public class ListListener {
    @Autowired
    private ListService listService;

    //上架
    @RabbitListener(bindings =
        @QueueBinding(
                value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
                exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
                key = MqConst.ROUTING_GOODS_UPPER
        )
    )
    public void onSale(Long skuId, Message message, Channel channel){
        //手动应答
        //参数1:消息的唯一标识
        //参数2:是否批量确实
        try {
            //接受skuId
            listService.onSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            // e.printStackTrace();
            if (!message.getMessageProperties().isRedelivered()){
                //第一次接受消息 给一次重新发生的机会
                //参数3:重新返回队列
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                //重试消息,重新发送消息机会用完
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                    log.error("添加索引失败:"+skuId);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }


    //下架
    @RabbitListener(bindings =
        @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
            key = MqConst.ROUTING_GOODS_LOWER
        )
    )
    public void cancelSale(Long skuId, Message message, Channel channel){
        //手动应答
        //参数1:消息的唯一标识
        //参数2:是否批量确实
        try {
            //接受skuId
            listService.cancelSale(skuId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            // e.printStackTrace();
            if (!message.getMessageProperties().isRedelivered()){
                //第一次接受消息 给一次重新发生的机会
                //参数3:重新返回队列
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                //重试消息,重新发送消息机会用完
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                    log.error("删除索引失败:"+skuId);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }
}
